Fk:loadTranslationTable{
  ["lzsj__chongtian"] = "冲天",
  ["#lzsj__chongtian-discard"] = "冲天：你可弃置%arg张牌",
  [":lzsj__chongtian"] = "一名角色的非额外回合结束后，若你于此回合失去过至少两张牌或受到过伤害，你可选择弃置等量的牌或减少等量体力上限，然后进行一个额外的回合。",
  ["lzsj__chongtian1"] = "弃置%arg张牌",
  ["lzsj__chongtian2"] = "减少%arg点体力上限",
  
  ["$lzsj__chongtian1"] = "冲天香阵透长安，满城尽带黄金甲！",
  ["$lzsj__chongtian2"] = "腐土越碎，越养得出冲天的野草！",
}

local chongtian = fk.CreateSkill{
  name = "lzsj__chongtian",
}

chongtian:addEffect(fk.TurnEnd, {
  anim_type = "special",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and not target:insideExtraTurn() then
      local room = player.room
      local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      local end_id = turn_event.id
      local ids = {}
      local n = 0
      room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from ~= nil and move.from == player and (move.to ~= player or (move.toArea ~= Card.PlayerHand and move.toArea ~= Card.PlayerEquip)) then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                table.insertIfNeed(ids, info.cardId)
              end
            end
          end
        end
      end, end_id)
      room.logic:getActualDamageEvents(1, function(e)
        local damage = e.data
        if damage.to == player then
          n = n + damage.damage
        end
      end)
      --self.cost_data = {#ids, n}
      event:setCostData(self, {card_num = #ids, damage = n})
      return #ids >= 2 or n > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local cost_data = event:getCostData(self)
    local n = cost_data.card_num
    local damage = cost_data.damage
    local choices = {"lzsj__chongtian1:::".. n, "lzsj__chongtian2:::".. damage, "Cancel"}
    local all_choices = {"lzsj__chongtian1:::".. n, "lzsj__chongtian2:::".. damage, "Cancel"}
    if n < 2 then table.removeOne(choices, all_choices[1]) end
    if damage == 0 then table.removeOne(choices, all_choices[2]) end
    local choice = player.room:askToChoice(player, {
      choices = choices,
      skill_name = chongtian.name,
    })
    if choice == all_choices[1] then
      local card = player.room:askToDiscard(player, {
        min_num = n,
        max_num = n,
        include_equip = true,
        skill_name = chongtian.name,
        prompt = "#lzsj__chongtian-discard:::"..n,
      })
      return #card > 0
    elseif choice == all_choices[2] then
      --player.room:changeMaxHp(player, -damage, chongtian.name)
      event:setCostData(self, {maxhp = damage})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local damage = event:getCostData(self).maxhp
    if damage then
      player.room:changeMaxHp(player, -damage, chongtian.name)
    end
    if player.dead then return end
    player:gainAnExtraTurn(true, chongtian.name)
  end
})

return chongtian

